package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.BusinessMapper;
import com.goods.business.service.BusinessService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;

    // 获取分页数据
    @Override
    public PageVO<ProductCategory> getCategoryTree(Integer pageNum, Integer pageSize) {
        // 1.从db中查询所有数据
        List<ProductCategory> productCategoryList = businessMapper.selectAll();

        // 2.复制实体类
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = new ArrayList<>();
        productCategoryList.forEach(productCategory -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
            productCategoryTreeNodeVOList.add(productCategoryTreeNodeVO);
        });

        // 3.构建三级分类的对象
        List<ProductCategoryTreeNodeVO> vos = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOList);
        System.out.println(vos);
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOList);
        System.out.println(build);

        // 4.分页查询三级分类的对象
        if(pageNum!=null && pageSize!=null){
            PageHelper.startPage(pageNum, pageSize);
        }
        PageInfo pageInfo = new PageInfo(build);
        PageVO productCategoryPageVO = new PageVO<>(pageInfo.getTotal(),build);

        // 返回分页数据
        return productCategoryPageVO;
    }

    // 获取父级分类数据
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree(){
        // 1.从db中查询所有数据
        List<ProductCategory> productCategoryList = businessMapper.selectAll();

        // 2.复制实体类
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = new ArrayList<>();
        productCategoryList.forEach(productCategory -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
            productCategoryTreeNodeVOList.add(productCategoryTreeNodeVO);
        });

        // 3.构建三级分类的对象
        List<ProductCategoryTreeNodeVO> vos = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOList);
        System.out.println(vos);

        // 返回数据
        return vos;
    }

    // 添加分类
    @Override
    public void add(ProductCategory productCategory) {
        // 创建时间和修改时间
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        // 向数据库新增一条数据
        businessMapper.insert(productCategory);
    }

    // 修改分类-回显数据-根据id获取分类信息
    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        return businessMapper.selectOne(productCategory);
    }

    // 修改分类
    @Override
    public void update(Long id, ProductCategory productCategory) {
        businessMapper.updateByPrimaryKey(productCategory);
    }

    // 删除分类
    @Override
    public void delete(Long id) {
        // 根据id递归删除分类
        reciveHaveChildren(id);
    }

    // 根据id递归删除分类
    public void reciveHaveChildren(Long id){
        ProductCategory productCategory = new ProductCategory();
        // 设置父id
        productCategory.setPid(id);
        // 查询是否有数据得父id为当前id
        List<ProductCategory> select = businessMapper.select(productCategory);
        // 如果有 接着往下查看 是否还有
        if(select!=null){
            for (ProductCategory category : select) {
                reciveHaveChildren(category.getId());
            }
        }
        // 没有 根据id删除数据
        System.out.println(id);
        productCategory = new ProductCategory();
        productCategory.setId(id);
        businessMapper.delete(productCategory);
        return;
    }
}
